package com.holdtime.jtt809dataservice.business.hd.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.holdtime.common.utils.general.ApiResponseModel;
import com.holdtime.framework.tablestore.core.service.TableStoreService;
import com.holdtime.framework.tablestore.core.util.Md5Utils;
import com.holdtime.jtt809.taxi.module.entity.CarRegisterDO;
import com.holdtime.jtt809.taxi.module.entity.LocationNewDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.CarRegisterMapper;
import com.holdtime.jtt809.taxi.module.entity.mapper.LocationNewMapper;
import com.holdtime.jtt809.taxi.module.nosql.model.TsLocationInfo;
import com.holdtime.jtt809dataservice.business.api.CarInfoService;
import com.holdtime.jtt809dataservice.common.bean.CarInfoRegisterReqDTO;
import com.holdtime.jtt809dataservice.common.bean.CarInfoReqDTO;
import com.holdtime.jtt809dataservice.common.bean.CarRealLocationReqDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 *  author: sunwei
 *  Date: 2025/3/17 09:13
 *  Description:
 */
@Slf4j
@Service
public class CarInfoServiceImpl implements CarInfoService {
    @Resource
    private CarRegisterMapper carRegisterMapper;
    @Resource
    private LocationNewMapper locationNewMapper; // 位置信息
    @Resource
    private TableStoreService tableStoreService;

    @Override
    public ApiResponseModel<?> dealCarInfoRegisterReq(CarInfoReqDTO<CarInfoRegisterReqDTO> carInfoReqDTO) {
        ApiResponseModel model = new ApiResponseModel<>();
        CarRegisterDO carRegister = new CarRegisterDO();
        carRegister.setPlatformId(carInfoReqDTO.getCarInfoData().getPlatformId());
        carRegister.setProducerId(carInfoReqDTO.getCarInfoData().getProducerId());
        carRegister.setTerminalModelType(carInfoReqDTO.getCarInfoData().getTerminalModelType());
        carRegister.setTerminalId(carInfoReqDTO.getCarInfoData().getTerminalId());
        carRegister.setSimCode(carInfoReqDTO.getCarInfoData().getTerminalSimcode());
        carRegister.setVehicleNo(carInfoReqDTO.getVehicleNo());
        carRegister.setVehicleColor(String.valueOf(carInfoReqDTO.getVehicleColor()));
        CarRegisterDO oldCarRegister = carRegisterMapper.selectOne(CarRegisterDO::getVehicleNo, carRegister.getVehicleNo(),
                CarRegisterDO::getVehicleColor, carRegister.getVehicleColor());
        if (null == oldCarRegister) {
            carRegister.setCreateTime(LocalDateTime.now());
            carRegister.setUpdateTime(LocalDateTime.now());
            carRegisterMapper.insert(carRegister);
        }else{
            carRegister.setId(oldCarRegister.getId());
            carRegister.setUpdateTime(LocalDateTime.now());
            carRegisterMapper.updateById(carRegister);
        }
        return model.success();
    }

    @Override
    public ApiResponseModel<?> dealCarRealLocationReq(CarRealLocationReqDTO carLoginReqDTO) {
        ApiResponseModel model = new ApiResponseModel<>();
        LocationNewDO locationNewDO = BeanUtil.toBean(carLoginReqDTO, LocationNewDO.class);
        LambdaQueryWrapperX<LocationNewDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(LocationNewDO::getVehicleNo, locationNewDO.getVehicleNo());
        queryWrapper.eq(LocationNewDO::getUploadTime, locationNewDO.getUploadTime());
        if (locationNewMapper.exists(queryWrapper)) {
            locationNewMapper.update(locationNewDO, queryWrapper);
        } else {
            locationNewMapper.insert(locationNewDO);
        }
        saveLocationToTs(locationNewDO);
        return model.success();
    }

    public void saveLocationToTs(LocationNewDO locationNewDO) {
        TsLocationInfo locationInfo = new TsLocationInfo();
        String partkey = locationNewDO.getVehicleNo() + locationNewDO.getVehicleColor()
                + LocalDateTimeUtil.format(locationNewDO.getUploadTime(), "MMdd");
        partkey = Md5Utils.backMD5(partkey).substring(0, 4);
        locationInfo.setPartKey(partkey);
        locationInfo.setVehicleNo(locationNewDO.getVehicleNo());
        locationInfo.setUploadTime(locationNewDO.getUploadTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        locationInfo.setVehicleColor(locationNewDO.getVehicleColor());
        locationInfo.setLon(locationNewDO.getLon());
        locationInfo.setLat(locationNewDO.getLat());
        locationInfo.setSps(Integer.valueOf(locationNewDO.getSps()));
        locationInfo.setDrs(Integer.valueOf(locationNewDO.getDrs()));
        locationInfo.setMileage(locationNewDO.getMileage());
        locationInfo.setDirection(Integer.valueOf(locationNewDO.getDirection()));
        locationInfo.setAltitude(Integer.valueOf(locationNewDO.getAltitude()));
        locationInfo.setState(locationNewDO.getState());
        locationInfo.setAlarm(locationNewDO.getAlarm());
        try {
            tableStoreService.putData(locationInfo);
        } catch (Exception e) {
            log.error("保存位置数据到表格存储异常", e);
        }

    }
}
